package com.office.onlineInformation.allMessage.controller;


import com.office.onlineInformation.allMessage.pojo.OaOnlineMessage;
import com.office.onlineInformation.allMessage.service.AllMessageService;
import com.office.personalOffice.personalInfo.model.LoginUser;
import com.office.personalOffice.personalInfo.model.TbStaff;
import com.office.util.PageModel;
import com.office.util.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

import static com.office.onlineInformation.draftBox.controller.draftBoxController.getUser;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 88168
 * @since 2022-10-25
 */
@Controller
@RequestMapping("/allMessage")
public class AllMessageController {
    @Autowired
    public AllMessageService service;
    @Autowired
    public RedisCache redisCache;
    @Autowired
    public RedisTemplate redisTemplate;
    @Autowired
    public CacheManager cacheManager;


    /**
     * 删除留言
     * @param informationId
     * @return
     */
    @RequestMapping("/delete/{informationId}")
    @ResponseBody
    public String deleteMessage(@PathVariable int informationId){
        String message = service.deleteMessage(informationId);
        return message;
    }
    /**
     * 回复留言
     * @param onlineMessage
     * @return
     */
    @RequestMapping("/update")
    @ResponseBody
    public String updateMessage(@RequestBody  OaOnlineMessage onlineMessage){
        String message = service.updateMessage(onlineMessage);
        return message;
    }

    /**
     * 查询所有留言
     * @return
     */
    @RequestMapping("/getList")
    @ResponseBody
    public PageModel<OaOnlineMessage> getList( OaOnlineMessage customManage, PageModel<OaOnlineMessage> pageModel, Authentication authentication){
        String key = "email";
        PageModel<OaOnlineMessage>  res =( PageModel<OaOnlineMessage>) redisTemplate.opsForValue().get(key);
        if (res!=null){
            return res;
        }
        Authentication authentication1 = SecurityContextHolder.getContext().getAuthentication();
        System.out.println("_______________________"+authentication1);
        TbStaff user = getUser(authentication);
        customManage.setSender(user.getName());
        PageModel<OaOnlineMessage> list = service.getList(customManage, pageModel);
        redisTemplate.opsForValue().set(key,list);
        return list;
    }
    /**
     * 查询所有留言
     * @return
     */
    @RequestMapping("/getPage")
    @ResponseBody
    public PageModel<OaOnlineMessage> getPage(OaOnlineMessage customManage, PageModel<OaOnlineMessage> pageModel, Authentication authentication){
        TbStaff user = getUser(authentication);
        customManage.setSender(user.getName());
        PageModel<OaOnlineMessage> list = service.getList(customManage, pageModel);
        return list;
    }
    /**
     * 根据id查询留言
     * @param informationId
     * @return
     */
    @RequestMapping("/getById/{informationId}")
    public String getById(@PathVariable int informationId, Model model){
        OaOnlineMessage message = service.getById(informationId);
        model.addAttribute("message",message );
        return "onlineInformation/allMessage/MessageView.html";
    }
    /**
     * 根据id查询留言-回复留言
     * @param Id
     * @return
     */
    @RequestMapping("/getOne/{Id}")
    @ResponseBody
    public OaOnlineMessage getById2(@PathVariable int Id){
        OaOnlineMessage message = service.getById(Id);
        return message;
    }

    public static TbStaff getUser(Authentication authentication){
        LoginUser principal =(LoginUser) authentication.getPrincipal();
        TbStaff user = principal.getUser();
        return user;
    }
}

